package persistencia;

import java.sql.*;
import javax.swing.JOptionPane;

public class DAO {
	
   private Connection conn;
   private PreparedStatement pstmt;
   private ResultSet rs;


   public Connection getConexao() {
        if (conn == null) {
            try {
                String url = "jdbc:mysql://localhost:3306/siscef_db";
                String user = "root";
                String password = "";
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection(url, user, password);
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
                conn = null;
            } catch (ClassNotFoundException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
                conn = null;
            }
        }
        return conn;
   }
   
   public PreparedStatement getPstmt() {
       return pstmt;
   }

   public void setPstmt(PreparedStatement pstmt) {
        this.pstmt = pstmt;
   }
   
   public ResultSet getRs() {
        return rs;
   }

   public void setRs(ResultSet rs) {
        this.rs = rs;
   }
   
   public void executeQuery(String query) {
        try {
             setPstmt(getConexao().prepareStatement(query));
             setRs(getPstmt().executeQuery());
        } catch (SQLException ex) {
             JOptionPane.showMessageDialog(null, ex.getMessage());
        }
   }

}